﻿@page "/"

@inject IAssignmentService AssignmentService
@inject ICourseService CourseService
@inject IAnnouncementService AnnouncementService

@inject IConfiguration Configuration

<PageTitle>作业管理平台</PageTitle>

<GridRow Gutter="@gutter">
    <GridCol Xs="GridProperty(24,1) " Sm="GridProperty(10,0) ">
        <AuthorizeView Roles="教师,学生,管理员">
            <Authorized>
                @if (context.User.IsInRole("学生") || context.User.IsInRole("教师"))
                {
                     <div style="margin-bottom:16px">
                    <AntList Bordered DataSource="@_assignmnets" Size="small">
                        <Header>作业列表</Header>
                        <ChildContent Context="item">
                            <ListItem>
                                <span>
                                    [ @item.Deadline ]  <NavLink href="@($"/course/{@item.CourseId}")">@item.Title</NavLink>
                                </span>
                            </ListItem>
                        </ChildContent>
                        <Footer>相关作业数:@_assignmnets.Count</Footer>
                    </AntList>
                    </div>

                    if (context.User.IsInRole("学生"))
                    {
                        @if (_assignmnets.Any(a =>(new TimeSpan(a.Deadline.Ticks - DateTime.Now.Ticks)).TotalDays<1))
                        {
                                <Alert Type="@AlertType.Warning"
                                       Message="你有一个课程作业即将截止"
                                       ShowIcon="true"
                            />
                        }
                        else
                        {
                            <Alert Type="@AlertType.Success"
                                   Message="目前没有需要处理的事项"
                                   ShowIcon="true" />
                        }
                    }

                }
                else 
                {
                    <AntList Style="margin-bottom:16px" Bordered DataSource="@_courses" Size="small">
                        <Header>课程列表</Header>
                        <ChildContent Context="item">
                            <ListItem>
                                <span>
                                    [@item.CourseNumber]  <NavLink href="@($"/course/{@item.CourseId}")">@item.Name</NavLink>
                                </span>
                            </ListItem>
                        </ChildContent>
                        <Footer>
                            <Pagination Current="@_coursePage" DefaultPageSize="@COURSEEACHPAGE" Total="@_totalCount" OnChange="@OnPageChanged"  HideOnSinglePage="true"></Pagination>
                        </Footer>
                    </AntList>
                    <Alert Type="@AlertType.Success"
                           Message="目前没有需要处理的事项"
                           ShowIcon="true" />
                }


            </Authorized>
            <NotAuthorized>
                <div style="height:300px;width:stretch; background-color:white ; text-align:center; padding:64px">
                    <h2>欢迎来到这个站点</h2>
                    <p>你尚未登录，请:</p>
                    <UserButton></UserButton>
                </div>

            </NotAuthorized>
        </AuthorizeView>
    </GridCol>
    <GridCol Xs="GridProperty(24,0)"  Sm="GridProperty(14,1) ">
       <div style="margin-bottom:16px">
            <Carousel Autoplay="TimeSpan.FromSeconds(5)">
                <CarouselSlick>
                    <Image Width="100%" Height="40vh" Src="@Configuration.GetSection("AppSettings:LoopImage1").Value" />
                </CarouselSlick>
                <CarouselSlick>
                    <Image Width="100%" Height="40vh" Src="@Configuration.GetSection("AppSettings:LoopImage2").Value" />
                </CarouselSlick>
                <CarouselSlick>
                    <Image Width="100%" Height="40vh" Src="@Configuration.GetSection("AppSettings:LoopImage3").Value" />
                </CarouselSlick>
                <CarouselSlick>
                    <Image Width="100%" Height="40vh" Src="@Configuration.GetSection("AppSettings:LoopImage4").Value" />
                </CarouselSlick>
            </Carousel>

       </div>

        <div style="margin-bottom:16px">
           <GridRow Gutter="8">
                @for (int i = 0; i < 4; i++)
                {
                    int index = i;
                    <GridCol Xs="12" Md="6">
                        @if(_announcements.Count>0)
                        {                   
                            <Card Bordered Hoverable  Style="margin-bottom:8px;height:180px;overflow:hidden">
                                <TitleTemplate>
                                <Icon Type="@_announcements[index%_announcements.Count].Icon" Theme="outline" />
                                @_announcements[index%_announcements.Count].Title
                                </TitleTemplate>
                                <Extra>
                                    <a href="@($"/announcement/{_announcements[index%_announcements.Count].AnnouncementId}")">查看</a>
                                </Extra>
                                <Body>
                                @_announcements[index%_announcements.Count].SubTitle
                                </Body>
                            </Card>
                        }
                        else
                        {
                            <Card Title="公告" Bordered Hoverable>
                                <Body>
                                    <p>暂无公告</p>
                                </Body>
                            </Card>
                        }

                    </GridCol>
                }
            </GridRow>
           </div>
    </GridCol>

</GridRow>

<style>
    .ant-carousel .slick-slide {
        text-align: center;
        border:thin;
        border-width:2px;
/*        background: #364d79;*/
        overflow: hidden;
    }

        .ant-carousel .slick-slide h3 {
            color: #fff;
        }
</style>

@code{

    [CascadingParameter]
    private Task<AuthenticationState>? _authenticationState { get; set; }

    private List<Assignment> _assignmnets = new();

    private List<Models.Course> _courses = new();
    private int _totalCount = 0;
    private int _coursePage = 0;
    private const int COURSEEACHPAGE= 10;

    private List<Announcement> _announcements = new();

    protected override async Task OnParametersSetAsync()
    {
        var state= await _authenticationState;
        var user = state.User;

        if (user.IsInRole("教师") || user.IsInRole("学生"))
        {
            var userId =Guid.Parse(user.FindFirstValue(ClaimTypes.NameIdentifier));
            var response= await AssignmentService.GetLastByUserIdAsync(userId);
            if (response.Success)
            {
                _assignmnets = response.Data??new();
            }
        }
        else if(user.IsInRole("管理员"))
        {
            _courses = await CourseService.GetAllAsync(_coursePage, COURSEEACHPAGE);
            _totalCount = await CourseService.GetCount();
        }

        _announcements =await AnnouncementService.GetAllLatestAsync(0, 4);

        await base.OnParametersSetAsync();
    }

    private async void OnPageChanged(PaginationEventArgs e)
    {
        _courses = await CourseService.GetAllAsync(e.Page, COURSEEACHPAGE);
    }

    EmbeddedProperty GridProperty(int span, int order) => new() { Span = span, Order = order };
    Dictionary<string, int> gutter = new()
    {
        ["xs"] = 8,
        ["sm"] = 16,
        ["md"] = 24,
        ["lg"] = 32,
        ["xl"] = 48,
        ["xxl"] = 64
    };




}


